# -*- coding: utf-8 -*-

'''
Created on 05.06.2011

@author: mutius
'''
import wx
from wx._core import EVT_BUTTON
from wx._controls import EVT_TEXT
import DBEvents

class SearchDialog(wx.Dialog):
    def __init__(self, parent, id, cursor, currentTable, columns, grid):
        wx.Dialog.__init__(self, parent, id)
        self.cursor = cursor
        self.currentTable = currentTable
        self.columns = columns
        self.grid = grid
        self.command = str()
        self.label_2 = wx.StaticText(self, -1, u"поиск по таблице "+currentTable)
        self.static_line_1 = wx.StaticLine(self, -1)
        self.button_1 = wx.Button(self, -1, "Выход")
        self.labels, self.entrys = list(), list()

        self.command = str()
        self.SetTitle(u"поиск значений")
        self.SetSize((350, 180))

        for column in self.columns:
            self.labels.append(wx.StaticText(self, -1, column))
            self.entrys.append(wx.TextCtrl(self, -1, ""))
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)    
        
        #sizer_1.Add(self.checkbox_1, 0, wx.LEFT|wx.TOP, 10)
        sizer_1.Add(self.label_2, 0, wx.LEFT|wx.TOP, 10)
        sizer_1.Add(self.static_line_1, 0, wx.ALL|wx.EXPAND, 10)
        
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_4, 1, wx.EXPAND, 0)
        
        for label in self.labels:
            sizer_3.Add(label, 1, wx.LEFT|wx.RIGHT|wx.ALIGN_BOTTOM, 5)
        for entry in self.entrys:
            sizer_4.Add(entry, 1, wx.LEFT|wx.RIGHT|wx.ALIGN_BOTTOM, 5)
        
        sizer_1.Add(self.static_line_1, 0, wx.ALL|wx.EXPAND, 10)
        sizer_1.Add(self.button_1, 0, wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT, 10)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        
        self.Bind(EVT_BUTTON, self.onOk, self.button_1)
        for entry in self.entrys:
            self.Bind(EVT_TEXT, self.onGetChar, entry)
    
    def onGetChar(self, event):
        values = list()
        for entry in self.entrys:
            values.append(entry.GetValue())
            if len(values[-1]) > 0:
                if values[-1][0] == '"' or values[-1][0] == "'":
                    values[-1] = '"%' + values[-1][1:-1] + '%"'
                else:
                    values[-1] = '"%' + values[-1] + '%"'
        self.command, flag = u"SELECT * from " + self.currentTable + " WHERE ", False
        for i in xrange(len(self.columns)):
            if len(values[i]) > 0:
                flag = True
                self.command = self.command + self.columns[i] + " like " + values[i] + " && "
        if flag == True:
            self.command = self.command[0:-3]
            data = self.cursor.sendRequest(self.command)
        else:
            data = self.cursor.execGetAllFrom(self.currentTable)
        columns = self.cursor.getColumnLabels()
        self.grid.setData(data, colLabels=columns)
        self.grid.Refresh()
        
        
    
    def onOk(self, event):
        self.Close()